/**
 * Created by lenvov on 2017/3/17.
 */
(function (win) {
  var config = win.config;
  var  jqueryName = 'jquery-1.12.4.js';
  loadJS(config.staticDomain + '/static/common/lib/' + jqueryName,function () {
    $.getJSON(config.staticDomain + '/static/' + config.projectName + '/js/staticMap.json',function (staticMap) {
      loadCss(config.staticDomain + '/' + staticMap['app'].css);
      loadFavicon(config.staticDomain + '/static/common/images/favicon.ico');
      loadJS(config.staticDomain + '/' + staticMap['manifest'].js,function (){
        loadJS(config.staticDomain + '/' + staticMap['vendor'].js,function (){
          loadJS(config.staticDomain + '/' + staticMap['app'].js,function (){
          });
        });
      });
    });
  });

  function loadJS(url, success) {
    var domScript = document.createElement('script');
    domScript.src = url;
    success = success || function(){};
    domScript.onload = domScript.onreadystatechange = function() {
      if (!this.readyState || 'loaded' === this.readyState || 'complete' === this.readyState) {
        success();
        this.onload = this.onreadystatechange = null;
        this.parentNode.removeChild(this);
      }
    }
    document.getElementsByTagName('head')[0].appendChild(domScript);
  }

  function loadCss(url) {
    var link = document.createElement('link');
    link.rel="stylesheet";
    link.href = url;
    document.getElementsByTagName('head')[0].appendChild(link);
  }

  function loadFavicon(url) {
    var link = document.createElement('link');
    link.rel="shortcut icon";
    link.href = url;
    document.getElementsByTagName('head')[0].appendChild(link);
  }
})(window);
